package com.ruoyi.system.service.impl;

import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.testMapper;
import com.ruoyi.system.service.ITestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.OverridesAttribute;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

/**
 * @author Ly lilongfei
 * @date 2024/5/29
 * @Description
 */
@Service
public class TestService implements ITestService {

    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private testMapper testMapper;

    @Override
    public String test() {
        return "";
    }

    @Override
    public String drawLots(int start, int end, int numPeople, int minInterval,int group1, int group2, int group3) {
        if (end - start + 1 < numPeople) {
            throw new IllegalArgumentException("结束序号与起始序号之间不足以容纳所需抽取的人数。");
        }
        if (numPeople != group1 + group2 + group3) {
            throw new IllegalArgumentException("总人数不符");
        }

        List<Integer> drawnNumbers = new ArrayList<>();
        Random random = new Random();
        int currentNumber = start;

        while (drawnNumbers.size() < numPeople) {
            int interval;
            if (drawnNumbers.isEmpty()) {
                // 第一次的间隔为0到最大间隔
                interval = random.nextInt(minInterval * 2) ;
            } else {
                // 随机选择间隔，介于最小间隔和最小间隔的两倍之间
                interval = random.nextInt(minInterval * 2 - minInterval + 1) + minInterval;
            }
            int number = currentNumber + interval;

            // 确保号码不超出范围
            if (number > end) {
                number = end;
            }

            // 添加号码到结果列表
            drawnNumbers.add(number);

            // 更新下一次抽取的起始序号
            currentNumber = number + 1;
        }
        Collections.shuffle(drawnNumbers);
        // 创建三个列表用于存储分类结果
        List<Integer> category1 = new ArrayList<>();
        List<Integer> category2 = new ArrayList<>();
        List<Integer> category3 = new ArrayList<>();

        // 遍历列表，根据索引分类
        for (int i = 0; i < drawnNumbers.size(); i++) {
            if (i < group1) {
                category1.add(drawnNumbers.get(i));
            } else if (i < group2 + group1) {
                category2.add(drawnNumbers.get(i));
            } else {
                category3.add(drawnNumbers.get(i));
            }
        }
        List<List<Integer>> classified = new ArrayList<>();
        classified.add(category1);
        classified.add(category2);
        classified.add(category3);
        return classified.toString();
    }
    }


